package com.r2coding.music.controller;

import com.r2coding.common.enums.Module;
import com.r2coding.common.enums.OperationType;
import com.r2coding.common.util.Result;
import com.r2coding.music.annotation.SysLog;
import com.r2coding.music.entity.SongCategoryEntity;
import com.r2coding.music.service.SongCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author:98k灬
 * @since:2022/05/13 12:22
 **/
@RestController
@RequestMapping("/songCategory")
public class SongCategoryController {
    @Autowired
    SongCategoryService songCategoryService;

    /**
     * 添加歌曲分类信息
     *
     * @param entity 歌曲分类信息
     * @return 统一响应体
     */
    @SysLog(module = Module.MUSIC, operationType = OperationType.INSERT, description = "保存歌曲分类")
    @PreAuthorize("hasAuthority('songCategory:add')")
    @PostMapping("/save")
    public Result save(@RequestBody SongCategoryEntity entity) {
        entity.setDeleted(0);
        entity.setUpdateTime(new Date());
        entity.setCreateTime(new Date());
        songCategoryService.save(entity);
        return Result.ok();
    }

    /**
     * 删除歌曲分类信息
     *
     * @param id 歌曲分类id
     * @return 统一响应体
     */
    @SysLog(module = Module.MUSIC, operationType = OperationType.DELETE, description = "删除歌曲分类")
    @PreAuthorize("hasAuthority('songCategory:delete')")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        songCategoryService.removeById(id);
        return Result.ok();
    }

    /**
     * 更新歌曲分类信息
     *
     * @param entity 歌曲分类信息
     * @return 统一响应体
     */
    @SysLog(module = Module.MUSIC, operationType = OperationType.UPDATE, description = "修改歌曲分类")
    @PreAuthorize("hasAuthority('songCategory:edit')")
    @PutMapping("/update")
    public Result update(@RequestBody SongCategoryEntity entity) {
        entity.setUpdateTime(new Date());
        songCategoryService.updateById(entity);
        return Result.ok();
    }
}
